<?php
include('rootlogin.php');
include "../Classes\PHPExcel.php";
session_start();

/*	
			//excel
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->getFont()->setSize(16);
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "ADDFFF")), ) );
			$Q = str_replace("&#39;", "'", $Q);
			$Q = str_replace('&quot;', '"', $Q);
			$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row+2)," ".$Q);
			
			$A = str_replace("&#39;", "'", $A);
			$A = str_replace('&quot;', '"', $A);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->getFont()->setSize(14);
			$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row+2))->applyFromArray( 
			array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
										"color"        => array("rgb" => "e2e2e2")), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row+2)," ".$A);
			$excel_row++;
			//excel
	
	$sql = "SELECT name FROM `topic` WHERE `topicid` = '".$_SESSION['topicid']."'";
	$result = mysql_query($sql, $user_con);
	$tname = mysql_fetch_row($result);
	$filename = "../forum_data/".$tname[0]."_".$_SESSION['topicid']."_".$level.".xls";
	
	$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); 
	$objWriter->save($filename);
	
	
	header("location: video_intro2.php");	*/

?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="framestyle.css" type="text/css" media="screen">
<style type="text/css">

</style>
<script>
</script>
</head>

<body>
<div id="wrapper">
<div id="title">Online Listening Comprehension Program</div>

<div id="main">
<hr/>
<div id="title2">FORUM DATA</div>
<hr/><br><br>
<form method = POST action="index.php" >

<a href="../forum_data/forum.xls">download</a><br><br><br>
<?php
	// Export xls
	/** Error reporting */ 
	error_reporting(E_ALL); 	
	/** PHPExcel */ 
	require_once "../Classes\PHPExcel.php"; 	
	/** PHPExcel_IOFactory */ 
	require_once "../Classes\PHPExcel\IOFactory.php";
	$objPHPExcel = new PHPExcel(); 
	$objPHPExcel->setActiveSheetIndex(0);

	
	$filename = "../forum_data/forum.xls";
	
	$excel_row=1;
	
	
	
	
	$lvl[1]="EASY";
	$lvl[2]="INTERMEDIATE";
	$lvl[3]="DIFFICULT";
	$sql = "SELECT * FROM topic";
	$search = mysql_query($sql, $user_con);
	while($topic = mysql_fetch_array($search)){
		$level=1;	
		while($level<=3){
			echo "<span class='blue'>";
			echo strtoupper($topic['name']);
			echo ", ".$lvl[$level]."</span>";
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->getFont()->setSize(20);
			$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->applyFromArray( 
							array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "C7DCFF")), ) );
			$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row),strtoupper($topic['name']).", ".$lvl[$level]);
			$excel_row++;
			
			$query = "SELECT qid,question,type,color, user from forum_question WHERE topicid=".$topic['topicid']." AND level=".$level." order by timedate DESC";
			$result = mysql_query($query, $user_con) or die ('Error in query');
			while($row = mysql_fetch_row($result)){
				echo "<table width=900px style='background-color: #EFF5FF; border: solid 5px #EFF5FF'><tr valign=top><td width=50px>";
				if($row[2]==1){
					echo "<font  size=2pt face='Courier New' color = \"".$row[3]."\">[#".$row[3]."]</font>&nbsp;";
					echo "<td>Q: ".$row[1];
					$user = str_replace("&#39;", "'", $row[4]);
					$user = str_replace('&quot;', '"', $user);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row),$user);
								
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
							"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row)," Q ");
								
					$Q = str_replace("&#39;", "'", $row[1]);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->getStyle("C".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("c".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("C".($excel_row),$Q);
					$excel_row++;
				}
				else if($row[2]==2){
					echo "<font  size=2pt face=\"Courier New\" color = \"".$row[3]."\">[#".$row[3]."]</font>&nbsp;";
					echo "<td>D: ".$row[1];
					$user = str_replace("&#39;", "'", $row[4]);
					$user = str_replace('&quot;', '"', $user);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
											"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row),$user);
								
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
							"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row)," D ");
								
					$Q = str_replace("&#39;", "'", $row[1]);
					$Q = str_replace('&quot;', '"', $Q);
					$objPHPExcel->getActiveSheet()->getStyle("C".($excel_row))->getFont()->setSize(16);
					$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->applyFromArray( 
					array("fill"     => array("type"        => PHPExcel_Style_Fill::FILL_SOLID, 
														"color"        => array("rgb" => "EFF5FF")), ) );
					$objPHPExcel->getActiveSheet()->setCellValue("C".($excel_row),$Q);
					$excel_row++;
				}
				else 
					echo "error";
				echo "</table><table  width=900px style='background-color: #EFF5FF; border: solid 5px #EFF5FF' >";	
				$query2 = "SELECT rid,repo,type,color, user FROM forum_reply WHERE qid='$row[0]' AND  topicid=".$topic['topicid']." AND level=".$level." order by timedate";
				$result2 = mysql_query($query2, $user_con)or die ('Error in query');
				
				while($row2 = mysql_fetch_row($result2)){
					echo "<tr valign=top><td width=80px>";
					//echo "&emsp;&emsp;";
					if($row2[2]==1){
						echo "<td width=80px><font size=2pt face=\"Courier New\" color = \"".$row2[3]."\">[#".$row2[3]."]</font>&nbsp;";
						echo "<td><font size=4pt>R: ".$row2[1]."</font>";
						$user = str_replace("&#39;", "'", $row2[4]);
						$user = str_replace('&quot;', '"', $user);
						$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row),$user);
								
						$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row)," R ");
								
						$Q = str_replace("&#39;", "'", $row2[1]);
						$Q = str_replace('&quot;', '"', $Q);
						$objPHPExcel->getActiveSheet()->getStyle("C".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("C".($excel_row),$Q);
						$excel_row++;
					}
					else if($row2[2]==2){
						echo "<td width=50px><font size=2pt face=\"Courier New\" color = \"".$row2[3]."\">[#".$row2[3]."]</font>&nbsp;";
						echo "<td><font size=4pt>C: ".$row2[1]."</font>";
												$user = str_replace("&#39;", "'", $row2[4]);
						$user = str_replace('&quot;', '"', $user);
						$objPHPExcel->getActiveSheet()->getStyle("A".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("A".($excel_row),$user);
								
						$objPHPExcel->getActiveSheet()->getStyle("B".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("B".($excel_row)," C ");
								
						$Q = str_replace("&#39;", "'", $row2[1]);
						$Q = str_replace('&quot;', '"', $Q);
						$objPHPExcel->getActiveSheet()->getStyle("C".($excel_row))->getFont()->setSize(16);
						$objPHPExcel->getActiveSheet()->setCellValue("C".($excel_row),$Q);
						$excel_row++;
					}
			
				}
				echo "</table>";
			}
			echo "<br>";
			$excel_row++;
			$level++;	
		}
		echo "<br><br>";
		$excel_row+=2;
	}

	$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5"); 
	$objWriter->save($filename);

?>



<br><br>
<div class="btn_right"><input type="submit" name = "submit" value = "back to index"></div>
</div>
</form>




</body>

